package com.ruoyi.project.system.uflo.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bstek.uflo.command.impl.jump.JumpNode;
import com.bstek.uflo.model.ProcessDefinition;
import com.bstek.uflo.model.task.Task;
import com.bstek.uflo.process.assign.Assignee;
import com.bstek.uflo.service.ProcessService;
import com.bstek.uflo.service.TaskService;
import com.ruoyi.common.utils.ObjectUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.uflo.model.dto.HistoryTaskDTO;
import com.ruoyi.project.system.uflo.model.dto.TodoTaskDTO;
import com.ruoyi.project.system.uflo.model.vo.CensorVO;
import com.ruoyi.project.system.uflo.model.vo.ExamineVO;
import com.ruoyi.project.system.uflo.service.IUfloHistoryTaskService;
import com.ruoyi.project.system.uflo.service.IUfloTaskService;
import com.ruoyi.project.system.user.domain.User;

/**
 * 流程实例
 * @author xianz
 *
 */
@Controller
@RequestMapping("/ufloTask")
public class UfloTaskController extends BaseController{

	private String prefix = "system/uflo";
	
	@Autowired
	TaskService taskService;
	
	@Autowired
	IUfloTaskService ufloTaskService;
	
	@Autowired
	ProcessService processService;
	
	@Autowired
	IUfloHistoryTaskService uflohistoryTaskService;
	
	
	/**
	 * 我的代办
	 * @return
	 */
	@GetMapping("/todo")
	public String todo() {
		return prefix+"/todo";
	}
	
	/**
     * 查询我的代办列表
     */
    @PostMapping("/todoList")
    @ResponseBody
    public TableDataInfo todoList(TodoTaskDTO todoTaskDTO)
    {
        startPage();
        User user = getSysUser();
        List<TodoTaskDTO> list = ufloTaskService.selectTodoList(todoTaskDTO,user);
        return getDataTable(list);
    }
	
    
	
	/**
	 * 我的已办
	 * @return
	 */
	@GetMapping("/history")
	public String history() {
		return prefix+"/history";
	}
	
	
	/**
	 * 审核
	 * @return
	 */
	@PostMapping("/examine")
	@ResponseBody
	public AjaxResult examine(ExamineVO examineVO) {
		ufloTaskService.examine(examineVO);
    	return success();
	}
	
	/**
	 * 审核
	 * @return
	 */
	@GetMapping("/examine/{taskId}")
	public String examine(@PathVariable("taskId") Long taskId, ModelMap mmap) {
		
		//获取task
		Task task = taskService.getTask(taskId);
		
		//获取流程
		ProcessDefinition processDefinition = processService.getProcessById(task.getProcessId());
		mmap.addAttribute("key", processDefinition.getKey());
		
		//获取下一步骤节点
		List<String> nodes = taskService.getAvaliableAppointAssigneeTaskNodes(taskId);
		mmap.addAttribute("taskId", taskId);
		mmap.addAttribute("nodes", nodes);
		
		//获取任务节点
		List<JumpNode> jumpNodeList = taskService.getAvaliableRollbackTaskNodes(taskId);
		
		List<String> jumpNodes = new ArrayList<String>();
		
		for (JumpNode sumpNode : jumpNodeList) {
			String name = sumpNode.getName();
			if(!name.equals("开始")) {
				jumpNodes.add(name);
			}
		}
		
		mmap.addAttribute("jumpNodes", jumpNodes);
		
		if(ObjectUtils.isNotEmpty(nodes)) {
			//获取第一个节点的处理人
			List<Assignee> assignees = ufloTaskService.getTaskNodeAssignees(taskId, nodes.get(0));
			mmap.addAttribute("assignees", assignees);
		}
		
		//获取任务处理记录
		List<HistoryTaskDTO> historyTasks = uflohistoryTaskService.getHistoryTasks(task.getProcessInstanceId());
		mmap.addAttribute("historyTasks", historyTasks);
		
		return "system/uflo/examine";
		
	}
    /**
     * 送审
     * @return
     */
    @GetMapping("/censor/{taskId}")
    public String censor(@PathVariable("taskId") Long taskId, ModelMap mmap) {
    	
    	//获取下一步骤节点
    	List<String> nodes = taskService.getAvaliableAppointAssigneeTaskNodes(taskId);
    	mmap.addAttribute("taskId", taskId);
    	mmap.addAttribute("nodes", nodes);
    	
    	if(ObjectUtils.isNotEmpty(nodes)) {
    		//获取第一个节点的处理人
    		List<Assignee> assignees = ufloTaskService.getTaskNodeAssignees(taskId, nodes.get(0));
    		mmap.addAttribute("assignees", assignees);
    	}
    	
		return "system/uflo/censor";
    	
    }
	
    
	/**
	 * 根据任务id及任务节点名称获取任务处理人列表
	 * @return
	 */
    @PostMapping("/getTaskNodeAssignees")
    @ResponseBody
    public AjaxResult getTaskNodeAssignees(Long taskId,String taskNodeName) {
    	List<Assignee> assignees = ufloTaskService.getTaskNodeAssignees(taskId, taskNodeName);
        return success(assignees);
    }
	
    /**
     * 送审
     * @return
     */
    @PostMapping("/censor")
    @ResponseBody
    public AjaxResult censor(CensorVO censorVO) {
    	ufloTaskService.censor(censorVO);
    	return success();
    }
    
}
